import React from "react";
import Welcome from "@/pages/Account/robotSetup/welcome";
import { doWelcomeEdit, fetchWelcomeInfo } from "@/services/robotManagement";
import { Button, message } from "antd";
import { formatMessage } from 'umi-plugin-react/locale';
class WelcomeSetting extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      welcomeType: 1,
			welcomeEditor: "",
			welcomeInput: "",
      welcomeList: [],
    }
  }

  componentDidMount(){
    this.getWelcomeInfo()
  }

  getWelcomeInfo = async () => {
    const { applicationId } = this.props;
    const res = await fetchWelcomeInfo({applicationId})
    let welcomeList = res.personalizedWelcome ? res.personalizedWelcome : [];
    welcomeList.unshift({
      id: "default",
      userTypeId: "default",
      welcomeType: res.welcomeType,
      welcomeContent: res.welcome,
      questionList: res.questionList ? res.questionList : [],
    });
    this.Welcome.initInfo(welcomeList);
  }

  doSaveWelcome = async () => {
    const { applicationId } = this.props;
    let obj = this.Welcome.getWelcomeList()
    obj.applicationId = applicationId
    const res = await doWelcomeEdit(obj)
    if (res.code === 0) {
      this.getWelcomeInfo();
    }
    message.info(res.message)
  }

  render(){
    const { welcomeList } = this.state;
    return(
      <div>
        <Welcome
          welcomeList={welcomeList}
          onRef={(ref) => (this.Welcome = ref)}
        />
        <Button type="primary" onClick={this.doSaveWelcome} style={{marginTop:24,marginLeft:40}}>{formatMessage({ id: 'account.robotSetup.welcome.save' })}</Button>
      </div>
    )
  }
}

export default WelcomeSetting